package com.example.bloom.myapplication;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by bloom on 2017/3/23.
 */

public class MyReportsAdapter extends BaseRecycleViewAdapter implements View.OnClickListener {

    private Context mContext;
//    private List<ReportBean> mDatas;

    public MyReportsAdapter(Context context) {
        this.mContext = context;
//        this.mDatas = lists;
    }

    public MyReportsAdapter(Context context, List<ReportBean> lists) {
        this.mContext = context;
//        this.mDatas = lists;
    }

    public interface OnRecyclerViewItemClickListener {
        void onItemClick(View view, int position);
    }

    private OnRecyclerViewItemClickListener mOnItemClickListener = null;

    public void setRecycleItemClickEvent(OnRecyclerViewItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

//    @Override
//    public int getItemViewType(int position) {
//        return super.getItemViewType(position);
//    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(mContext).inflate(R.layout.layout_item_work_report_message, parent, false);
        ViewHolder holder = new ViewHolder(view);
        holder.ivRelay.setOnClickListener(this);
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        ReportBean report = (ReportBean) datas.get(position);
        ViewHolder myViewHolder = (ViewHolder) holder;

        myViewHolder.tvReportType.setText(report.getReportType() == null ? "" : report.getReportType());
        myViewHolder.tvReportTime.setText(report.getReportTime() == null ? "" : report.getReportTime());
        myViewHolder.tvName.setText(report.getUserName());
        myViewHolder.tvTime.setText(report.getSubmitTime() == null ? "" : report.getSubmitTime());
        myViewHolder.tvContent.setText(report.getReportContent() == null ? "" : report.getReportContent());
        myViewHolder.tvApprovedPerson.setText(report.getReadOver() == null ? "" : report.getReadOver());
        myViewHolder.tvCopyName.setText(report.getCarbonCopy() == null ? "" : report.getCarbonCopy());

        myViewHolder.ivRelay.setTag(position);

        if (report.hasReply()) {
            setReplyContents(myViewHolder, report.getReplys());
        }
    }

    private void setReplyContents(ViewHolder holder, List<ReportReplyBean> replys) {

        if (replys != null && replys.size() > 0) {
            holder.mReplys.clear();
            holder.mReplys.addAll(replys);
            holder.mReplyMessageAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public int getItemCount() {
        return  datas.size();
    }

    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null) {
            mOnItemClickListener.onItemClick(v, (Integer) v.getTag());
        }
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        ImageView ivAvatar;
        TextView tvReportType;
        TextView tvReportTime;
        TextView tvName;
        TextView tvTime;
        TextView tvContent;
        TextView tvApprovedPerson;
        TextView tvCopyName;

        ImageView ivRelay;

        RecyclerView mReplyRecyclerView;
        ReplyMessageAdapter mReplyMessageAdapter;
        List<ReportReplyBean> mReplys;
        LinearLayoutManager mLinearLayoutManager;

        public ViewHolder(View itemView) {
            super(itemView);

            ivAvatar = (ImageView) itemView.findViewById(R.id.iv_avatar);
            tvReportType = (TextView) itemView.findViewById(R.id.tv_report_type);
            tvReportTime = (TextView) itemView.findViewById(R.id.tv_report_time);
            tvName = (TextView) itemView.findViewById(R.id.tv_name);
            tvTime = (TextView) itemView.findViewById(R.id.tv_time);
            tvContent = (TextView) itemView.findViewById(R.id.tv_report_content);
            tvApprovedPerson = (TextView) itemView.findViewById(R.id.tv_approvedperson);
            tvCopyName = (TextView) itemView.findViewById(R.id.tv_copyperson);

            ivRelay = (ImageView) itemView.findViewById(R.id.iv_relay);
            mReplyRecyclerView = (RecyclerView) itemView.findViewById(R.id.rlv_reply);
            initReplyView();
        }

        private void initReplyView() {
            mLinearLayoutManager = new LinearLayoutManager(mContext);
            mLinearLayoutManager.setStackFromEnd(true);
            mReplyRecyclerView.setLayoutManager(mLinearLayoutManager);

            mReplys = new ArrayList<>();
            mReplyMessageAdapter = new ReplyMessageAdapter(mContext, mReplys);
            mReplyRecyclerView.setAdapter(mReplyMessageAdapter);
        }
    }


}